/*
 * @Author: syyao 
 * @Date: 2019-05-17 15:22:58 
 * @Last Modified by: syyao
 * @Last Modified time: 2019-06-12 21:14:46
 */
import axios from 'axios';
import store from '../store'
import router from '../router'
//每次传过去的cookie不变
axios.defaults.withCredentials = true;


//对未登录的请求进行拦截
axios.interceptors.response.use(
    response => {
        if (response.data.code === '600') {
            console.log('loginFilter');
            //如果已经拦截过就不拦截
            if (store.state.loginFilter===true){
                return response;
            }
            router.push({name:'home'})
            store.commit('setLoginFilter');
            
        } else {
            return response;
        }

    },
    error => {
        return Promise.reject(error)
    }
)

function post(_this, url, param) {
    return new Promise((resolve, reject) => {
        axios.post(_this.postIp + url, param)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err)
                _this.$Notice.error({
                        title: '出错了',
                        render: h => {
                            return h('span', [
                                '你可以点击 ',
                                h('a', {
                                        value: '这里',
                                        on: {
                                            click: () => {
                                                console.log(1);
                                                _this.$Modal.info({
                                                    width: 600,
                                                    render: h => {
                                                        return h('div', {
                                                            style: {
                                                                fontFamily: 'console'
                                                            }
                                                        }, [h('h1', '接口名称:' + ' ' + url),
                                                            h('h3', {style: {marginTop: '5px', marginBottom: '5px'}}, '报错信息'),
                                                            h('Input', {
                                                                props: {
                                                                    value: err
                                                                }
                                                            }), h('h3', {
                                                                style: {
                                                                    marginTop: '5px',
                                                                    marginBottom: '5px'
                                                                }
                                                            }, '请求数据'), h('Input', {
                                                                props: {
                                                                    value: JSON.stringify(err.response.data),
                                                                    type: 'textarea',
                                                                    autosize: true,
                                                                },
                                                            }), h('h3', {
                                                                style: {
                                                                    marginTop: '5px',
                                                                    marginBottom: '5px'
                                                                }
                                                            }, '请求信息'), h('Input', {
                                                                props: {
                                                                    value: JSON.stringify(err.response.request),
                                                                    type: 'textarea',
                                                                    autosize: true,
                                                                },
                                                            })])

                                                    }
                                                })
                                            }
                                        }
                                    }, '这里'
                                ),
                                ' 查看'
                            ])
                        },
                        duration:
                            3
                    }
                )
                ;
            })
    })
}

export default {post}
